package com.rayson.yututu.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rayson.yututu.annotation.AuthCheck;
import com.rayson.yututu.common.BaseResponse;
import com.rayson.yututu.constant.UserConstant;
import com.rayson.yututu.manager.auth.SpaceUserAuthManager;
import com.rayson.yututu.model.entity.Space;
import com.rayson.yututu.model.enums.SpaceLevel;
import com.rayson.yututu.model.enums.SpaceLevelEnum;
import com.rayson.yututu.model.req.IdRequest;
import com.rayson.yututu.model.req.space.SpaceAddReq;
import com.rayson.yututu.model.req.space.SpaceEditReq;
import com.rayson.yututu.model.req.space.SpaceListReq;
import com.rayson.yututu.model.req.space.SpaceUpdateReq;
import com.rayson.yututu.model.vo.SpaceVO;
import com.rayson.yututu.service.SpaceService;
import com.rayson.yututu.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: rayson
 * Description: 空间相关接口
 * Date: 2025-01-11 12:02
 */
@Slf4j
@RestController
@RequestMapping("/space")
public class SpaceController {


    @Resource
    private SpaceService spaceService;

    @Resource
    private SpaceUserAuthManager spaceUserAuthManager;

    @Resource
    private UserService userService;


    @GetMapping("/list")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<SpaceVO>> listSpacePage(SpaceListReq spaceListReq) {
        return BaseResponse.success(spaceService.listSpacePage(spaceListReq));
    }

    @PostMapping("/add")
    @AuthCheck
    public BaseResponse<String> addSpace(@RequestBody SpaceAddReq spaceAddReq) {
        return BaseResponse.success(spaceService.addSpace(spaceAddReq));
    }

    @PostMapping("/edit")
    @AuthCheck
    public BaseResponse<Void> editSpace(@RequestBody @Valid SpaceEditReq spaceEditReq) {
        spaceService.editSpace(spaceEditReq);
        return BaseResponse.success(null, "操作成功");
    }

    @PostMapping("/admin/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Void> updateSpaceByAdmin(@RequestBody @Valid SpaceUpdateReq spaceUpdateReq) {
        spaceService.updateSpaceByAdmin(spaceUpdateReq);
        return BaseResponse.success(null, "操作成功");
    }

    @PostMapping("/delete")
    @AuthCheck
    public BaseResponse<Void> deleteSpace(@RequestBody IdRequest idRequest) {
        if (StrUtil.isBlank(idRequest.getId())) {
            return BaseResponse.fail("缺少必要参数");
        }
        boolean result = spaceService.removeById(idRequest.getId());
        log.info("删除空间，id:{}, 数据库执行结果:{}", idRequest.getId(), result);
        return BaseResponse.success(null, "操作成功");
    }

    @GetMapping("/vo/{id}")
    public BaseResponse<SpaceVO> getSpaceVOById(@PathVariable String id, HttpServletRequest request) {
        Space space = spaceService.getById(id);
        SpaceVO spaceVO = Space.toVO(space);
        spaceVO.setPermissionList(spaceUserAuthManager.getPermissionList(space, userService.getLoginUser(request)));
        return BaseResponse.success(spaceVO);
    }

    @GetMapping("/spaceLevel")
    public BaseResponse<List<SpaceLevel>> listSpaceLevel() {
        List<SpaceLevel> spaceLevelList = Arrays.stream(SpaceLevelEnum.values())
                .map(t -> new SpaceLevel(t.getName(), t.getName(), t.getMaxCount(), t.getMaxSize()))
                .collect(Collectors.toList());
        return BaseResponse.success(spaceLevelList);
    }

    @GetMapping("/current/space")
    public BaseResponse<SpaceVO> getCurrentUserSpace() {
        return BaseResponse.success(spaceService.getCurrentUserSpace());
    }

}
